package com.lemon.core.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

/**
 * @author 李猛
 * @datetime 2023/3/30 14:58
 * @description 获取HTTP请求信息
 */
@Slf4j
public class HttpUtil {
    /**
     * 获取请求中的IP地址
     *
     * @param request
     * @return
     */
    public static String getHttpIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        if ("0:0:0:0:0:0:0:1".equals(ip)) {
            try {
                ip = InetAddress.getLocalHost().getHostAddress();
            } catch (UnknownHostException e) {
                e.printStackTrace();
                return null;
            }
        }
        return ip;
    }

    /**
     * 获取真实客户端IP
     *
     * @param request
     * @return
     */
    public static String getHttpIp(ServerHttpRequest request) {
        String ipAddress;
        try {
            // 1.根据常见的代理服务器转发的请求ip存放协议，从请求头获取原始请求ip。值类似于203.98.182.163, 203.98.182.163
            ipAddress = request.getHeaders().getFirst("x-forwarded-for");
            if (!StringUtils.hasText(ipAddress) || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeaders().getFirst("Proxy-Client-IP");
            }
            if (!StringUtils.hasText(ipAddress) || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeaders().getFirst("WL-Proxy-Client-IP");
            }

            // 2.如果没有转发的ip，则取当前通信的请求端的ip
            if (!StringUtils.hasText(ipAddress) || "unknown".equalsIgnoreCase(ipAddress)) {
                InetSocketAddress remoteAddress = request.getRemoteAddress();
                if (remoteAddress != null) {
                    ipAddress = remoteAddress.getAddress().getHostAddress();
                }
                // 如果是127.0.0.1，则取本地真实ip
                if ("127.0.0.1".equals(ipAddress)) {
                    InetAddress localHost = InetAddress.getLocalHost();
                    if (localHost.getHostAddress() != null) {
                        ipAddress = localHost.getHostAddress();
                    }
                }
            }

            // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割 "***.***.***.***"
            if (ipAddress != null) {
                ipAddress = ipAddress.split(",")[0].trim();
            }
        } catch (Exception e) {
            log.error("~~~~~~~~~~~解析请求IP失败~~~~~~~~~~~,message:{}", e.getMessage());
            ipAddress = "";
        }
        return ipAddress == null ? "" : ipAddress;
    }
}
